import ComponentBase from "../engine/ComponentBase";
import DateUtil from "../engine/DateUtil";
import { Logger } from "../engine/Logger";
import { SpriteFrameComponent } from "../engine/SpriteFrameComponent";
import UIManager from "../engine/UIManager";
import { GameConfig } from "../game/config/GameConfig";
import { RoomConfig } from "../game/config/RoomConfig";
import { RoomConfig_cangxian_mj } from "../game/config/RoomConfig_cangxian_mj";
import { RoomConfig_ddz } from "../game/config/RoomConfig_ddz";
import { RoomConfig_huanghua_mj } from "../game/config/RoomConfig_huanghua_mj";
import { RoomConfig_jianziding } from "../game/config/RoomConfig_jianziding";
import { RoomConfig_paodekuai } from "../game/config/RoomConfig_paodekuai";
import { ViewConfig } from "../game/config/ViewConfig";
import { RoomGameInfo } from "../game/model/RoomGameInfo";
import { RoomModel } from "../game/model/RoomModel";
import { RoomPlayer } from "../game/model/RoomPlayer";
import { UIHelper } from "../game/util/UIHelper";
import ViewGameRule from "../game/view/ViewGameRule";
import { SocketClient } from "../net/SocketClient";


const { ccclass, property } = cc._decorator;

@ccclass
export default class GameUIBase extends ComponentBase {

    private menuNode: cc.Node;
    private btnDownMenu: cc.Node
    private btnUnRelax: cc.Node;
    private viewGameRule: ViewGameRule
    private btnReady: cc.Node;
    private txtLeftReady: cc.Label;

    // private txtRoomId: cc.Label
    // private txtJushu: cc.Label;
    // private txtCost: cc.Label;
    // private txtNow: cc.Label
    private txtMore: cc.Label
    private icon_playerLoc: cc.Node
    private btnLeaveRoom: cc.Node
    private btnReleaseRoom: cc.Node
    private startTime: number = 0

    public controlUI: cc.Node


    onEnable(): void {
        super.onEnable()
        this.controlUI = this.findChild(this.node, "controlUI")

        this.startTime = DateUtil.now()
        this.icon_playerLoc = this.findChild(this.node, "icon_playerLoc")
        UIHelper.onAddEvent(this.icon_playerLoc, () => {
            UIManager.instance.showView(ViewConfig.viewConfig.ViewRoomLocation)
        })

        let btnChat: cc.Node = this.findChild(this.node, "btnChat")


        this.btnUnRelax = this.findChild(this.node, "btnUnRelax")
        this.btnUnRelax.active = false
        UIHelper.onAddEvent(this.btnUnRelax, () => {
            SocketClient.req_trustee(false)
        })
        this.viewGameRule = this.getChild(this.node, "ViewGameRule", ViewGameRule)
        this.viewGameRule.node.active = false

        this.menuNode = this.findChild(this.node, "menuNode")
        this.menuNode.active = false;
        this.btnDownMenu = this.findChild(this.node, "btnDownMenu")
        UIHelper.onAddEvent(this.btnDownMenu, () => {
            this.menuNode.active = !this.menuNode.active
            this.refreshMenu()
        })
        this.refreshMenu()

        let btnSetting: cc.Node = this.findChild(this.menuNode, "btnSetting")
        UIHelper.onAddEvent(btnSetting, () => {
            this.menuNode.active = false
            this.refreshMenu()
            if (RoomModel.instance.gameType == GameConfig.Game_Type.huanghua_mj) {
                UIManager.instance.showView(ViewConfig.viewConfig.ViewSetting)
            }

        })
        let btnWanfa: cc.Node = this.findChild(this.menuNode, "btnWanfa")
        UIHelper.onAddEvent(btnWanfa, () => {
            this.menuNode.active = false;
            this.refreshMenu()
            this.viewGameRule.node.active = true
            this.viewGameRule.onInitChild(RoomModel.instance)
        })

        let btnReleaseRoom: cc.Node = this.findChild(this.menuNode, "btnReleaseRoom")
        this.btnReleaseRoom = btnReleaseRoom
        UIHelper.onAddEvent(btnReleaseRoom, () => {
            this.menuNode.active = false;
            this.refreshMenu()
            SocketClient.req_apply_dismiss()
        })

        let btnLeaveRoom: cc.Node = this.findChild(this.menuNode, "btnLeaveRoom")
        this.btnLeaveRoom = btnLeaveRoom
        UIHelper.onAddEvent(btnLeaveRoom, () => {
            this.menuNode.active = false;
            this.refreshMenu()
            SocketClient.req_leave_room()
        })

        this.btnReady = this.findChild(this.controlUI, "btnReady")
        this.btnReady.active = false
        UIHelper.onAddEvent(this.btnReady, () => {
            SocketClient.req_ready(true)
        })
        this.txtLeftReady = this.getChild(this.btnReady, "txtLeftReady", cc.Label)

        let infoNode: cc.Node = this.findChild(this.node, "roomInfoNode/infoNode")

        // this.txtRoomId = this.getChild(infoNode, "txtRoomId", cc.Label)
        // this.txtJushu = this.getChild(infoNode, "txtJushu", cc.Label)
        // this.txtNow = this.getChild(infoNode, "txtNow", cc.Label)
        // this.txtCost = this.getChild(infoNode, "txtCost", cc.Label)
        this.txtMore = this.getChild(infoNode, "txtMore", cc.Label)
        this.refreshRoomInfo()

    }

    private refreshMenu() {
        if (this.menuNode.active) {
            this.btnDownMenu.scaleY = 1
        } else {
            this.btnDownMenu.scaleY = -1
        }
    }

    protected update(dt: number): void {
        let myPlayer: RoomPlayer = RoomPlayer.getMyPlayer()
        if (myPlayer) {
            this.refreshPlayerReady(myPlayer);
            this.refreshClock(myPlayer)
        }
        // RoomModel.instance.roomType = RoomModel.instance.conf["roomType"]
        if (RoomModel.instance.roomType != RoomConfig.Room_Type.Create) {
            // if (RoomModel.instance.roomType == RoomConfig.Room_Type.Ziyou) {
            //     this.btnReleaseRoom.active = false;
            // }
            // if (RoomModel.instance.num_of_turns >= 1) {
            // this.btnReleaseRoom.active = false
            // this.btnLeaveRoom.active = false
            // }
            if (RoomModel.instance.num_of_turns < 1) {
                this.btnReleaseRoom.active = false
                this.btnLeaveRoom.active = true
            } else {
                this.btnReleaseRoom.active = true
                this.btnLeaveRoom.active = false
            }
        } else {
            if (RoomModel.instance.num_of_turns < 1) {
                this.btnReleaseRoom.active = true
                this.btnLeaveRoom.active = true
            } else {
                this.btnReleaseRoom.active = true
                this.btnLeaveRoom.active = false
            }
        }
    }

    private refreshRoomInfo() {
        this.txtMore.string = ""
        if (RoomModel.instance.isPlayVideo) {
            let pass: number = DateUtil.now() - this.startTime
            this.txtMore.string += DateUtil.formatDateStr(RoomModel.instance.videoInfo.time + pass)
        } else {
            this.txtMore.string += DateUtil.formatDateStr(DateUtil.now())
        }

        this.txtMore.string += " 房号:" + RoomModel.instance.room_id + ""
        // this.txtDifen.string = "开房底分:" + RoomModel.instance.conf.difen + ""

        if (RoomModel.instance.roomType == RoomConfig.Room_Type.Create) {
            if (RoomModel.instance.conf["cost_type"] == RoomConfig.cost_type.pingjun) {
                this.txtMore.string += " " + RoomConfig.cost_type_name[RoomModel.instance.conf["cost_type"]] + ":" + RoomModel.instance.conf["cost"] / RoomModel.instance.conf["renshu"] + "钻石"
            } else {
                this.txtMore.string += " " + RoomConfig.cost_type_name[RoomModel.instance.conf["cost_type"]] + ":" + RoomModel.instance.conf["cost"] + "钻石"
            }
        } else {
            this.txtMore.string += " " + "服务费:" + RoomModel.instance.conf["cost"] + "金豆"
        }
        // if (RoomModel.instance.conf["distance_limit"]) {
        //     txtDistance.string += "距离限制:" + RoomModel.instance.conf["distance_limit"] + "米"
        // } else {
        //     txtDistance.string += "距离限制:无限制"
        // }
        let confObj: object = RoomModel.instance["conf"]
        if (RoomModel.instance.gameType == GameConfig.Game_Type.huanghua_mj) {
            this.txtMore.string += " " + "仁怀麻将," + confObj["jushu"] + "局," + confObj["renshu"] + "人麻将"
            this.txtMore.string += "," + RoomConfig_huanghua_mj.jiTypeNameConfig[confObj["jiType"]]
            if (confObj["benbenji"] == 1) {
                this.txtMore.string += ",本本鸡"
            }
            if (confObj["jinyinwu"] == 1) {
                this.txtMore.string += ",金银乌"
            }
            if (confObj["jianqiwa"]) {
                this.txtMore.string += ",见七挖"
            }
            if (confObj["zhuangcheng"]) {
                this.txtMore.string += ",庄上必乘"
            }
            if (confObj["ruanyingji"]) {
                this.txtMore.string += ",软硬鸡"
            }
            if (confObj["ruanyingdou"]) {
                this.txtMore.string += ",软硬豆"
            }
            this.txtMore.string += ",见豆" + RoomConfig_huanghua_mj.jiandouConfig[confObj["jiandou"]]
            this.txtMore.string += ",大宽张" + RoomConfig_huanghua_mj.dakuanzhangConfig[confObj["dakuanzhang"]] + "+"
            this.txtMore.string += ",大对子" + RoomConfig_huanghua_mj.daduiziConfig[confObj["daduizi"]] + "+"
            this.txtMore.string += ",清一色" + RoomConfig_huanghua_mj.qingGangConfig[confObj["qinggang"]] + "+"
            this.txtMore.string += ",自摸" + RoomConfig_huanghua_mj.zimoConfig[confObj["zimo"]] + "+"
        }


    }


    private refreshPlayerReady(myPlayer: RoomPlayer) {
        // Logger.log("refreshPlayerReady===", RoomModel.instance.isPlayVideo, this.btnReady.active)
        if (RoomModel.instance.isPlayVideo) {
            this.btnReady.active = false
        } else {
            if (RoomModel.instance.game_info.game_state == RoomGameInfo.Status_Type.Free) {
                this.btnReady.active = !myPlayer.ready
            } else {
                this.btnReady.active = false
            }
            if (this.btnReady.active) {
                if (RoomModel.instance.num_of_turns <= 0) {
                    let leftReadyTime: number = 60 - Math.floor((DateUtil.now() - myPlayer.joinTime) / 1000)
                    if (leftReadyTime < 0) {
                        leftReadyTime = 0
                    }
                    this.txtLeftReady.node.active = true
                    this.txtLeftReady.string = leftReadyTime + "秒后不准备将被踢出"
                } else {
                    this.txtLeftReady.node.active = false
                    this.txtLeftReady.string = ""
                }
            }
        }
    }

    public refreshClock(myPlayer: RoomPlayer) {
        this.refreshRoomInfo()
        if (RoomModel.instance.isPlayVideo) {

        } else {
            if (RoomModel.instance.game_info.trusteeList[myPlayer.chair]) {
                if (this.btnUnRelax) {
                    this.btnUnRelax.active = true
                }
            } else {
                if (this.btnUnRelax) {
                    this.btnUnRelax.active = false
                }
            }

        }
    }

}